<?php

class Application_Model_Comment
{
    /**
     * @param int $productID
     * @param bool $confirm
     * @param bool $deleted
     * @return CommentPOJO[]
     */
    public static function getComments($productID = 0, $confirm = true, $deleted = false)
    {
        $dbAdapter = JDbAdapter::factory();

        $dbSelect = $dbAdapter->select()
            ->from('COMMENTS');
        if ($productID != 0) {
            $dbSelect->where('ProductID = ?', $productID);
        }
        $dbSelect->where('Confirmed = ?', $confirm);
        $dbSelect->where('Deleted = ?', $deleted);

        $assoc = $dbAdapter->fetchAll($dbSelect);
        $commentPOJOs = array();
        foreach ($assoc as $row) {
            $commentPOJO = new CommentPOJO($row);
            array_push($commentPOJOs, $commentPOJO);
        }

        return $commentPOJOs;
    }

    /**
     * @param $content
     * @param $author
     * @param $productID
     * @param $userID
     * @return int
     */
    public static function addComment($content, $author, $productID, $userID)
    {
        $dbAdapter = JDbAdapter::factory();

        try {
            $dbAdapter->insert(
                'COMMENTS',
                array(
                    'Content' => $content,
                    'Author' => $author,
                    'ProductID' => $productID,
                    'UserID' => $userID
                )
            );

            return (int)$dbAdapter->lastInsertId();
        } catch (Exception $ex) {
            JLog::log($ex->getMessage());
            return 0;
        }
    }

    /**
     * @param int[] $ids
     * @return bool
     */
    public static function deleteComments($ids)
    {
        $dbAdapter = JDbAdapter::factory();

        try {
            $str = '(';
            $length = count($ids);
            for ($i = 0; $i < $length - 1; $i++) {
                $str .= $ids[$i] . ', ';
            }
            $str .= $ids[$length - 1];
            $str .= ')';
            $numberOfAffects = $dbAdapter->update(
                'COMMENTS', array('Deleted' => true), "ID in $str"
            );
            if ($numberOfAffects == 0) {
                return false;
            }

            return true;
        } catch (Exception $ex) {
            JLog::log($ex->getMessage());
            return false;
        }
    }

    /**
     * @param int[] $ids
     * @return bool
     */
    public static function confirmComments($ids)
    {
        $dbAdapter = JDbAdapter::factory();

        try {
            $str = '(';
            $length = count($ids);
            for ($i = 0; $i < $length - 1; $i++) {
                $str .= $ids[$i] . ', ';
            }
            $str .= $ids[$length - 1];
            $str .= ')';
            $numberOfAffects = $dbAdapter->update(
                'COMMENTS', array('Confirmed' => true), "ID in $str"
            );
            if ($numberOfAffects == 0) {
                return false;
            }

            return true;
        } catch (Exception $ex) {
            JLog::log($ex->getMessage());
            return false;
        }
    }

    /**
     * @param int $id
     * @return bool
     */
    public static function unconfirmComment($id)
    {
        $dbAdapter = JDbAdapter::factory();

        try {
            $numberOfAffects = $dbAdapter->update(
                'COMMENTS', array('Confirmed' => false), "ID = $id"
            );
            if ($numberOfAffects == 0) {
                return false;
            }

            return true;
        } catch (Exception $ex) {
            JLog::log($ex->getMessage());
            return false;
        }
    }
}

class CommentPOJO extends POJO
{
    /** @var int */
    public $ID;

    /** @var string */
    public $Content;

    /** @var string */
    public $Author;

    /** @var Zend_Date */
    public $CreatedDate;

    /** @var bool */
    public $Confirmed;

    /** @var bool */
    public $Deleted;

    /** @var int */
    public $ProductID;

    /** @var int */
    public $UserID;

    /**
     * @return void
     */
    protected function convert()
    {
        $this->ID = (int)$this->ID;
        $this->Content = (string)$this->Content;
        $this->Author = (string)$this->Author;
        $this->CreatedDate = new Zend_Date($this->CreatedDate);
        $this->Confirmed = (bool)$this->Confirmed;
        $this->Deleted = (bool)$this->Deleted;
        $this->ProductID = (int)$this->ProductID;
        $this->UserID = (int)$this->UserID;
    }
}